﻿using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.TextGeneration;
using Microsoft.Extensions.DependencyInjection;

//create a builder
var builder = Kernel.CreateBuilder();

builder.AddAzureOpenAIChatCompletion("gpt-4o", "https://my-openapi.openai.azure.com/", "D6uw9hVI6vXTjGo35RTlEpTWbhfXd8xRs38tG2gIgWtvhNpxjJt4JQQJ99BDACYeBjFXJ3w3AAABACOGvPWE");

var kernel=builder.Build();

var chatCompletionService = kernel.Services.GetRequiredService<IChatCompletionService>();

var prompt = "你是一名专业的英语口语教练，请帮助我练习英语口语/n * 由于我的词汇量不足，因此我的表达中会中英混合，当出现这种情况是，请告诉我正确的英语表达/n * 由于我的语法薄弱，当表达出现语法错误时，请及时进行纠正。/n 由于我的词汇量不足，当我要求解释某些句子或单词时，用中文向我解释。/n 由于有些单词不会发音，当我询问时，以中文的形式告诉我发音技巧，比如单词 'hello'，的中文发音为'哈罗'";
var chatHistory=new ChatHistory(prompt);
string? userInput;
while (true)
{
    // Collect user input
    Console.Write("User > ");
    userInput = Console.ReadLine();
    if(string.IsNullOrEmpty(userInput))
    {
        break;
    }
    chatHistory.AddUserMessage(userInput);
    // Stream the AI response and add to chat history
    Console.WriteLine("AI Response:");
    var response = "";
    await foreach (var item in
        chatCompletionService.GetStreamingChatMessageContentsAsync(chatHistory))
    {
        Console.Write(item.Content);
        response += item.Content;
    }
    chatHistory.Add(new ChatMessageContent(AuthorRole.Assistant, response));
    Console.WriteLine();
}

